using SS;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
namespace SM.core
{
	internal class Point
	{
		private Func objFunc = new Func();
		private string strConnection = "";
		public Point()
		{
			Program.strConnectionString = this.objFunc.strConnectionString;
		}
		public int action(string sID, string classID, string teacherID, string Day, string Status)
		{
			int result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("ActionOnTabPoint", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("sID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("classID", SqlDbType.VarChar, 300);
				sqlCommand.Parameters.Add("teacherID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("Day", SqlDbType.DateTime);
				sqlCommand.Parameters.Add("Status", SqlDbType.NVarChar, 250);
				sqlCommand.Parameters["sID"].Value = sID;
				sqlCommand.Parameters["classID"].Value = classID;
				sqlCommand.Parameters["teacherID"].Value = teacherID;
				sqlCommand.Parameters["Day"].Value = Day;
				sqlCommand.Parameters["Status"].Value = Status;
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch
			{
				result = 1;
			}
			return result;
		}
		public bool checkPoint(string classID, DateTime day)
		{
			bool result = false;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string commandText = string.Concat(new object[]
			{
				"SELECT * FROM [cfb_db_user].[TabPoint] WHERE [classID] = '",
				classID,
				"' AND datepart(d,Day) = ",
				day.Day,
				" AND datepart(M,Day) = ",
				day.Month,
				" AND datepart(yyyy,Day) = ",
				day.Year
			});
			SqlCommand sqlCommand = sqlConnection.CreateCommand();
			sqlCommand.CommandText = commandText;
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				result = true;
			}
			sqlDataReader.Close();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public void fillToRow(DateTime day, ListView lv, int columnIndex, int rowIndex)
		{
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				string commandText = string.Concat(new object[]
				{
					"SELECT * FROM [TabPoint] WHERE [siD] = '",
					lv.Items[rowIndex].SubItems[0].Text,
					"' AND  [classID] = '",
					lv.Items[rowIndex].SubItems[1].Text.Replace("III", "3").Replace("II", "2").Replace("I", "1"),
					"' AND datepart(day,Day) = ",
					day.Day,
					" AND datepart(Month,Day) = ",
					day.Month,
					" AND datepart(year,Day) = ",
					day.Year
				});
				SqlCommand sqlCommand = sqlConnection.CreateCommand();
				sqlCommand.CommandText = commandText;
				SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
				while (sqlDataReader.Read())
				{
					lv.Items[rowIndex].SubItems.Add(sqlDataReader["Status"].ToString());
				}
				sqlDataReader.Close();
				sqlConnection.Close();
				sqlConnection.Dispose();
			}
			catch
			{
			}
		}
		public string[] listDayHeader()
		{
			string[] array = new string[100];
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string commandText = "SELECT DISTINCT convert(varchar, [Day], 103) AS DayOK FROM [TabPoint]";
			SqlCommand sqlCommand = sqlConnection.CreateCommand();
			sqlCommand.CommandText = commandText;
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			int num = 0;
			while (sqlDataReader.Read())
			{
				array[num] = sqlDataReader["DayOK"].ToString();
				num++;
			}
			sqlDataReader.Close();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return array;
		}
		public string[] listDay()
		{
			string[] array = new string[100];
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string commandText = "SELECT DISTINCT [Day] AS DayOK FROM [TabPoint]";
			SqlCommand sqlCommand = sqlConnection.CreateCommand();
			sqlCommand.CommandText = commandText;
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			int num = 0;
			while (sqlDataReader.Read())
			{
				array[num] = sqlDataReader["DayOK"].ToString();
				num++;
			}
			sqlDataReader.Close();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return array;
		}
		public string[] listclassID(string teacherID)
		{
			string[] array = new string[100];
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string commandText = "SELECT DISTINCT [classID] FROM [cfb_db_user].[TabPoint] WHERE [teacherID] = '" + teacherID + "'";
			SqlCommand sqlCommand = sqlConnection.CreateCommand();
			sqlCommand.CommandText = commandText;
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			int num = 0;
			while (sqlDataReader.Read())
			{
				array[num] = sqlDataReader["classID"].ToString();
				num++;
			}
			sqlDataReader.Close();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return array;
		}
		public void fillDayRow(string classID, string teacherID, ListView lv, int orderRow)
		{
			int maxCountDay = this.getMaxCountDay(this.listclassID(teacherID), teacherID);
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string commandText = string.Concat(new string[]
			{
				"SELECT DISTINCT [Day] FROM [cfb_db_user].[TabPoint] WHERE [classID] = '",
				classID,
				"' AND [teacherID] = '",
				teacherID,
				"'"
			});
			SqlCommand sqlCommand = sqlConnection.CreateCommand();
			sqlCommand.CommandText = commandText;
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			int num = 0;
			while (sqlDataReader.Read())
			{
				if (orderRow == 0)
				{
					lv.Items.Add(sqlDataReader["Day"].ToString().Substring(0, 10));
				}
				else
				{
					lv.Items[num].SubItems.Add(sqlDataReader["Day"].ToString().Substring(0, 10));
				}
				num++;
			}
			sqlDataReader.Close();
			sqlConnection.Close();
			sqlConnection.Dispose();
			if (num < maxCountDay)
			{
				if (orderRow == 0)
				{
					for (int i = num; i < maxCountDay; i++)
					{
						lv.Items.Add("");
					}
				}
				else
				{
					for (int i = num; i < maxCountDay; i++)
					{
						lv.Items[i].SubItems.Add("");
					}
				}
			}
		}
		public void delDay(DateTime day)
		{
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string commandText = string.Concat(new object[]
			{
				"DELETE FROM [TabPoint] WHERE datepart(day,Day) = ",
				day.Day,
				" AND datepart(Month,Day) = ",
				day.Month,
				" AND datepart(year,Day) = ",
				day.Year
			});
			SqlCommand sqlCommand = sqlConnection.CreateCommand();
			sqlCommand.CommandText = commandText;
			sqlCommand.ExecuteNonQuery();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public int getCountDay(string classID, string teacherID)
		{
			int result = 0;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string commandText = string.Concat(new string[]
			{
				"SELECT COUNT(DISTINCT [Day]) AS COUNTDAY FROM [cfb_db_user].[TabPoint] WHERE [classID] = '",
				classID,
				"' AND [teacherID] = '",
				teacherID,
				"'"
			});
			SqlCommand sqlCommand = sqlConnection.CreateCommand();
			sqlCommand.CommandText = commandText;
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				result = int.Parse(sqlDataReader["COUNTDAY"].ToString());
			}
			sqlDataReader.Close();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public int getMaxCountDay(string[] classID, string teacherID)
		{
			int num = 0;
			int[] array = new int[100];
			for (int i = 0; i < classID.Length; i++)
			{
				if (classID[i] != null)
				{
					array[num] = this.getCountDay(classID[i], teacherID);
					num++;
				}
			}
			int num2 = array[0];
			for (int j = 0; j < array.Length; j++)
			{
				if (array[j] != 0)
				{
					if (num2 < array[j])
					{
						num2 = array[j];
					}
				}
			}
			return num2;
		}
		public int[] getlistCountDay(string[] classID, string teacherID)
		{
			int num = 0;
			int[] array = new int[100];
			for (int i = 0; i < classID.Length; i++)
			{
				if (classID[i] != null)
				{
					array[num] = this.getCountDay(classID[i], teacherID);
					num++;
				}
			}
			return array;
		}
		public float[] getlistSalery(string[] classID, string teacherID)
		{
			int num = 0;
			float[] array = new float[100];
			for (int i = 0; i < classID.Length; i++)
			{
				if (classID[i] != null)
				{
					array[num] = this.listSaleryOfTeacherWithClass(classID[i], teacherID);
					num++;
				}
			}
			return array;
		}
		public float listSaleryOfTeacherWithClass(string classID, string teacherID)
		{
			float result = 0f;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string commandText = string.Concat(new string[]
			{
				"SELECT [salery] FROM [cfb_db_user].[TabSalery] WHERE [classID] = '",
				classID,
				"' AND [teacherID] = '",
				teacherID,
				"'"
			});
			SqlCommand sqlCommand = sqlConnection.CreateCommand();
			sqlCommand.CommandText = commandText;
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				try
				{
					result = float.Parse(sqlDataReader["salery"].ToString());
				}
				catch
				{
					result = 0f;
				}
			}
			sqlDataReader.Close();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public void loadSaleryToListView(ListView lv, string teacherID)
		{
			lv.Items.Clear();
			int num = 0;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string commandText = "SELECT * FROM [cfb_db_user].[TabSalery] WHERE [teacherID] = '" + teacherID + "'";
			SqlCommand sqlCommand = sqlConnection.CreateCommand();
			sqlCommand.CommandText = commandText;
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				lv.Items.Add(sqlDataReader["classID"].ToString());
				lv.Items[num].UseItemStyleForSubItems = false;
				lv.Items[num].SubItems.Add(sqlDataReader["Salery"].ToString());
				num++;
			}
			sqlDataReader.Close();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public int updateSalery(string teacherID, string classID, float salery)
		{
			int result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("ActionOnTabSalery", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("teacherID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("classID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("salery", SqlDbType.Float);
				sqlCommand.Parameters["teacherID"].Value = teacherID;
				sqlCommand.Parameters["classID"].Value = classID;
				sqlCommand.Parameters["salery"].Value = salery;
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch
			{
				result = 1;
			}
			return result;
		}
		public void loadToListView_Point(ListView lv)
		{
			lv.Items.Clear();
			int num = 0;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT [UserName],[FullName] FROM [TabUser]";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				lv.Items.Add("");
				lv.Items[num].SubItems.Add(sqlDataReader["FullName"].ToString());
				lv.Items[num].UseItemStyleForSubItems = true;
				lv.Items[num].BackColor = Color.Aqua;
				num++;
				lv.Items.Add(sqlDataReader["UserName"].ToString());
				lv.Items[num].UseItemStyleForSubItems = false;
				lv.Items[num].SubItems.Add("I");
				num++;
				lv.Items.Add(sqlDataReader["UserName"].ToString());
				lv.Items[num].UseItemStyleForSubItems = false;
				lv.Items[num].SubItems.Add("II");
				num++;
				lv.Items.Add(sqlDataReader["UserName"].ToString());
				lv.Items[num].UseItemStyleForSubItems = false;
				lv.Items[num].SubItems.Add("III");
				num++;
			}
			sqlDataReader.Close();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
	}
}
